/*
 * 文件名：CellHouseServiceImpl.java 版权：Copyright by www.huawei.com 描述： 修改人：Administrator
 * 修改时间：2016年4月6日 跟踪单号： 修改单号： 修改内容：
 */

package com.zcsy.manage.service.base.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zcsy.manage.model.base.CellHouse;
import com.zcsy.manage.model.base.CellUnit;
import com.zcsy.manage.model.base.OwnerHouse;
import com.zcsy.manage.model.property.CarBill;
import com.zcsy.manage.model.system.Dict;
import com.zcsy.manage.persistence.base.CellHouseDao;
import com.zcsy.manage.service.base.ICellHouseService;
import com.zcsy.manage.service.base.ICellUnitService;
import com.zcsy.manage.service.system.IDictService;
import com.zcsy.manage.service.system.impl.SystemComService;

@Service
public class CellHouseServiceImpl extends SystemComService implements ICellHouseService {

	//手机号码和固话号码都能通过验证 支持长途，手机号码前可加0，固话可带区号
	Pattern phoneReg = Pattern.compile("^(0?1[358]\\d{9})$|^((0(10|2[1-3]|[3-9]\\d{2}))?[1-9]\\d{6,7})$");
	Pattern number = Pattern.compile("^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$");
	
	/*public static void main(String[] args) {
		Pattern phoneReg = Pattern.compile("^(0?1[358]\\d{9})$|^((0(10|2[1-3]|[3-9]\\d{2}))?[1-9]\\d{6,7})$");
		String temp = "256203525320";
		Matcher perPriceMatcher = phoneReg.matcher(temp);
    	System.out.println(perPriceMatcher.matches());
	}*/
	
    @Autowired
    private CellHouseDao cellHouseDao;
    
    @Autowired
    private ICellUnitService cellUnitService;

	@Autowired
	private IDictService dictService;
	
    @Override
    public int insert(CellHouse cellHouse) {
        return cellHouseDao.insert(cellHouse);
    }

    @Override
    public int update(CellHouse cellHouse) {
    	CellHouse oldHouse = cellHouseDao.selectByPrimaryKey(cellHouse.getId());
    	
    	oldHouse.setName(cellHouse.getName());
    	oldHouse.setHouseType(cellHouse.getHouseType());
    	oldHouse.setHouseState(cellHouse.getHouseState());
    	oldHouse.setArea(cellHouse.getArea());
    	oldHouse.setPerPrice(cellHouse.getPerPrice());
    	oldHouse.setOwnerName1(cellHouse.getOwnerName1());
    	oldHouse.setOwnerPhone1(cellHouse.getOwnerPhone1());
    	oldHouse.setOwnerName2(cellHouse.getOwnerName2());
    	oldHouse.setOwnerPhone2(cellHouse.getOwnerPhone2());
    	oldHouse.setUserAccount(cellHouse.getUserAccount());
    	oldHouse.setUpdateTime(cellHouse.getUpdateTime());
    	oldHouse.setRegionId(cellHouse.getRegionId());
    	oldHouse.setRegionName(cellHouse.getRegionName());
    	oldHouse.setBuildingId(cellHouse.getBuildingId());
    	oldHouse.setBuildingName(cellHouse.getBuildingName());
    	oldHouse.setUnitId(cellHouse.getUnitId());
    	oldHouse.setUnitName(cellHouse.getUnitName());
    	oldHouse.setMemo(cellHouse.getMemo());
        return cellHouseDao.update(oldHouse);
    }

    @Override
    public int deleteByPrimaryKey(String[] ids) {
        return cellHouseDao.deleteByPrimaryKey(ids);
    }

    @Override
    public CellHouse selectByPrimaryKey(String id) {
        return cellHouseDao.selectByPrimaryKey(id);
    }

    @Override
    public List<CellHouse> getAllCellHouse() {
        return cellHouseDao.getAllCellHouse();
    }

    @Override
    public List<CellHouse> selectByKeyword(CellHouse cellHouse) {
        return cellHouseDao.selectByKeyword(cellHouse);
    }

    @Override
    public int updateById(String id, String name, int type) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", id);
        map.put("name", name);
        map.put("type", type);
        return cellHouseDao.updateById(map);
    }

    @Override
    public List<CellHouse> getCellHouseByUnitId(String unitId) {
        return cellHouseDao.getCellHouseByUnitId(unitId);
    }

    /*
     * (non-Javadoc)
     * @see
     * com.zcsy.manage.service.base.ICellHouseService#selectForImport(com.zcsy.manage.model.base
     * .CellHouse)
     */
    @Override
    public List<CellHouse> selectForImport(CellHouse record) {
        return cellHouseDao.selectForImport(record);
    }

	@Override
	public int insertOwnerHouse(OwnerHouse record) {
		return cellHouseDao.insertOwnerHouse(record);
	}

	@Override
	public List<CellHouse> getListByOwnerIdAndState(String ownerId,
			Integer state) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ownerId", ownerId);
		map.put("state", state);
		return cellHouseDao.getListByOwnerIdAndState(map);
	}

	
	@Override
	public CellHouse getHousePropertyInfo(String houseId) {
		return cellHouseDao.getHousePropertyInfo(houseId);
	}
	
	@Override
	public int updateOHouseStateByOwnerIdAndHouseId(String cellId,
			String houseId,int state) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("cellId", cellId);
		map.put("houseId", houseId);
		map.put("state", state);
		return cellHouseDao.updateOHouseStateByOwnerIdAndHouseId(map);
	}

	@Override
	public OwnerHouse getByOHouseStateByOwnerIdAndHouseId(String ownerId,String houseId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ownerId", ownerId);
		map.put("houseId", houseId);
		return cellHouseDao.getByOHouseStateByOwnerIdAndHouseId(map);
	}

	@Override
    public List<OwnerHouse> getMyHouseList(String ownerId) {
        return cellHouseDao.getMyHouseList(ownerId);
    }
	
	@Override
	public int deleteOwnerHouseByHouseId(String houseId ,String ownerId) {
        return cellHouseDao.deleteOwnerHouseByHouseId(houseId,ownerId);

	}

	@Override
	public List<CellHouse> getCellHouses(String ownerId) {
		return cellHouseDao.getCellHouses(ownerId);
	}
	
	@Override
    public List<CellHouse> selectHouseInfoAndCarNoByOwnerId(String ownerId) {
        return cellHouseDao.selectHouseInfoAndCarNoByOwnerId(ownerId);
    }
	
	@Override
    public List<CarBill> getOwnerAllCars(String ownerId) {
        return cellHouseDao.getOwnerAllCars(ownerId);
    }
	
	@Override
	public List<CellHouse> selectHouseInfoCarBillByOwnerIdCellIdState(String ownerId, Integer houseState, String cellId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ownerId", ownerId);
		map.put("houseState", houseState);
		map.put("cellId", cellId);
		return cellHouseDao.selectHouseInfoCarBillByOwnerIdCellIdState(map);
	}

    @Override
    public List<CellHouse> selectByKeyWordCommand(CellHouse cellHouse) {
        return cellHouseDao.selectByKeywordCommand(cellHouse);
    }

	@Override
	public List<String> findBug(String cellId, List<List> list) {
		List<String> errorMsg = new ArrayList<String>();
		String cHouseString = null;
		Set<String> cHouseSet = new HashSet<String>();
		
		if(!list.isEmpty()) {   
            int size = list.size();
            
			//生成小区单元地址和小区单元对象的map集合
            List<CellUnit> cellUnits = cellUnitService.getCellUnitByCellId(cellId);
			Map<String, CellUnit> map = new HashMap<String, CellUnit>();
			for (CellUnit item : cellUnits) {
				String key = new StringBuffer(item.getRegionName()).append(item.getBuildingName()).append(item.getName()).toString();
				map.put(key, item);
			}

			//将当前小区的所有小区房屋地址拼成字符串，插入set集合中，方便查重
			List<CellHouse> cellHouses = cellHouseDao.getCellHouseByCellId(cellId);
			for (CellHouse item : cellHouses) {
				cHouseString = new StringBuffer(item.getRegionName()).append(item.getBuildingName()).append(item.getUnitName()).append(item.getName()).toString();
				cHouseSet.add(cHouseString);
			}
			
            for(int i = 2; i <= (size+1); i++) {
                List record = list.get(i-2);
                //判断不为空
                if (StringUtils.isEmpty(record.get(0).toString())) {
                	errorMsg.add("表格第"+i+"行第1列第不能为空！");
                    continue;
                }
                if (StringUtils.isEmpty(record.get(1).toString())) {
                	errorMsg.add("表格第"+i+"行第2列不能为空！");
                    continue;
                }
                if (StringUtils.isEmpty(record.get(2).toString())) {
                	errorMsg.add("表格第"+i+"行第3列不能为空！");
                    continue;
                }
                if (StringUtils.isEmpty(record.get(3).toString())) {
                	errorMsg.add("表格第"+i+"行第4列不能为空！");
                    continue;
                }
                if (StringUtils.isEmpty(record.get(4).toString())) {
                	errorMsg.add("表格第"+i+"行第5列不能为空！");
                    continue;
                }
                if (StringUtils.isEmpty(record.get(5).toString())) {
                	errorMsg.add("表格第"+i+"行第6列不能为空！");
                    continue;
                }
                
                //判断区域楼栋单元是否存在
                String unitKey = new StringBuffer(record.get(0).toString()).append(record.get(1).toString()).append(record.get(2).toString()).toString();
            	if (map.containsKey(unitKey) == false) {
            		errorMsg.add("表格第"+i+"行第3列所输单元不存在！");
            		continue;
            	}
	            
	            //判断区域楼栋单元房间是否存在
            	cHouseString = new StringBuffer(record.get(0).toString()).append(record.get(1).toString()).append(record.get(2).toString()).append(record.get(3).toString()).toString();
            	if (cHouseSet != null && cHouseSet.contains(cHouseString) == true) {
            		errorMsg.add("表格第"+i+"行第4列所输房号已存在！");
            		continue;
            	}
	            cHouseSet.add(cHouseString);
	            
                //判断房间类型是否正确
                List<Dict> houseTypes = dictService.getDictFromCache("houseType");
                int flag = 0;
                for(Dict item : houseTypes){
                	if(record.get(4).toString().equals(item.getLabel())){
                		flag++;
                		continue;
                	}
                }
                if (flag == 0) {
                	errorMsg.add("表格第"+i+"行第5列不正确！");
                	continue;
                }
                //判断房间状态是否正确
                List<Dict> houseStates = dictService.getDictFromCache("houseState");
                flag = 0;
                for(Dict item : houseStates){
                	if(record.get(5).toString().equals(item.getLabel())){
                		flag++;
                		continue;
                	}
                }
                if (flag == 0) {
                	errorMsg.add("表格第"+i+"行第6列不正确！");
                	continue;
                }
                //判断面积是否为正数
                if (!StringUtils.isEmpty(record.get(6).toString())) {
	            	Matcher areaMatcher = number.matcher(record.get(6).toString());
	            	if(!areaMatcher.matches()){
	            		errorMsg.add("表格第"+i+"行第7列应为正数！");
	            		continue;
	            	}
                }
            	//判断物业费单价是否为正数
                if (!StringUtils.isEmpty(record.get(7).toString())) {
	            	Matcher perPriceMatcher = number.matcher(record.get(7).toString());
	            	if(!perPriceMatcher.matches()){
	            		errorMsg.add("表格第"+i+"行第8列应为正数！");
	            		continue;
	            	}
                }
            	//判断电话格式是否正确
            	if (!StringUtils.isEmpty(record.get(9).toString())) {
            		/*if (StringUtils.isEmpty(record.get(8).toString())) {
                    	errorMsg.add("表格第"+i+"行姓名不能为空！");
                        continue;
                    }*/
            		Matcher phoneMatcher = phoneReg.matcher(record.get(9).toString());
                	if(!phoneMatcher.matches()){
                		errorMsg.add("表格第"+i+"行第10列电话格式不正确！");
                		continue;
                	}
				}
            	//判断电话2格式是否正确
            	if (!StringUtils.isEmpty(record.get(11).toString())) {
            		/*if (StringUtils.isEmpty(record.get(10).toString())) {
                    	errorMsg.add("表格第"+i+"行姓名2不能为空！");
                        continue;
                    }*/
            		Matcher phone2Matcher = phoneReg.matcher(record.get(11).toString());
            		if(!phone2Matcher.matches()){
            			errorMsg.add("表格第"+i+"行第12列电话格式不正确！");
            			continue;
            		}
				}
            }
        } else {
            errorMsg.add("无数据");
        }
        return errorMsg;
	}

	@Override
	public int insertAll(List<CellHouse> cellHouses) {
		return cellHouseDao.insertAll(cellHouses);
	}

    @Override
    public boolean isNameExists(String houseId, String unitId, String name) {
        return cellHouseDao.isNameExists(houseId, unitId, name);
    }

    @Override
    public Integer countApply(String cellId) {
        return cellHouseDao.countApply(cellId);
    }
   
}
